package com.bitsemart.dialogos;

/**
 * Created by Roberto on 20/05/2015.
 */

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.bitsemart.MainActivity;
import com.bitsemart.R;
import com.bitsemart.VisorVideojuego.EditorVideojuegoAdmin;

public class InputTextDialogFragment extends DialogFragment {
    EditText userInput;
    public final static int TYPE_ADMIN_MODE = 0;
    public final static int TYPE_ENTER_PATH = 1;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View promptsView = getActivity().getLayoutInflater().inflate(R.layout.input_text_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        int type = getArguments().getInt("TYPE");
        userInput = (EditText) promptsView.findViewById(R.id.input);
        userInput.setText(EditorVideojuegoAdmin.getImage_path());
        switch (type){
            case TYPE_ADMIN_MODE:
                userInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                builder
                        .setTitle("Insertar contraseña administrativa:")
                        .setView(promptsView)
                        .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        })
                        .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                if (MainActivity.entrarModoAdministrador(userInput.getText().toString())){
                                    MainActivity.mostrarMensaje(getActivity(), "Has entrado al modo administrador");
                                } else {
                                    MainActivity.mostrarMensaje(getActivity(), "Contraseña incorrecta");
                                }
                            }
                        });
                break;
            case TYPE_ENTER_PATH:
                userInput.setInputType(InputType.TYPE_CLASS_TEXT);
                builder.setTitle("Introduce la ruta de la imagen:");
                builder.setView(promptsView);
                builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                builder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Bitmap newImage = BitmapFactory.decodeFile(userInput.getText().toString());
                        if (newImage==null) {
                            MainActivity.mostrarMensaje(getActivity(), "La ruta especificada no es correcta");
                        } else {
                            EditorVideojuegoAdmin.setImage_path(userInput.getText().toString());
                            EditorVideojuegoAdmin.mImage.setImageBitmap(newImage);
                        }
                    }
                });
                break;
        }

        return builder.create();
    }
}
